﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProjektXNA
{
    class Tower : Sprite
    {
        public int cost { get; set; }
        public int damage { get; set; }
        public float range { get; set; }
        public Enemy target { get; set; }

        public Tower(Texture2D texture, Vector2 position)
            : base(texture, position)
        {
            range = 1000;
        }

        public bool inRange(Vector2 position)
        {
            if (Vector2.Distance(center, position) <= range)
                return true;

            return false;
        }

        public void nearestEnemy(List<Enemy> enemies)
        {
            target = null;
            float smallestRange = range;

            foreach (Enemy enemy in enemies)
            {
                if (Vector2.Distance(center, enemy.Center) < smallestRange)
                {
                    smallestRange = Vector2.Distance(center, enemy.Center);
                    target = enemy;
                }
            }
        }

        protected void turn()
        {
            Vector2 direction = center - target.Center;
            direction.Normalize();

            rotation = (float)Math.Atan2(-direction.X, direction.Y);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (target != null)
                turn();
        }

    }
}